package com.eedi.framework.dept.dal.mapper;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.dept.controller.org.vo.OrgDeptListReq;
import com.eedi.framework.dept.controller.org.vo.OrgDeptPageReq;
import com.eedi.framework.dept.dal.dataobject.OrgDeptDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface OrgDeptMapper extends BaseMapperX<OrgDeptDO> {

    default List<OrgDeptDO> selectList(OrgDeptListReq reqVO) {
        return selectList(new LambdaQueryWrapperX<OrgDeptDO>()
                .likeIfPresent(OrgDeptDO::getOrgDeptName, reqVO.getOrgDeptName())
                .eqIfPresent(OrgDeptDO::getOrgDeptStatus, reqVO.getOrgDeptStatus())
                .orderByAsc(OrgDeptDO::getOrgDeptSort));
    }

    default void updateStatusByOrgDeptId(String orgDeptId, CommonStatusEnum orgDeptStatus) {
        update(new OrgDeptDO().setOrgDeptStatus(orgDeptStatus), new LambdaQueryWrapperX<OrgDeptDO>()
                .eq(OrgDeptDO::getOrgDeptId, orgDeptId));
    }

    default OrgDeptDO selectByParentIdAndName(String parentOrgDeptId, String orgDeptName) {
        return selectOne(OrgDeptDO::getParentOrgDeptId, parentOrgDeptId, OrgDeptDO::getParentOrgDeptId, orgDeptName);
    }

    default Long selectCountByParentId(String parentOrgDeptId) {
        return selectCount(OrgDeptDO::getParentOrgDeptId, parentOrgDeptId);
    }

    default List<OrgDeptDO> selectListByParentId(Collection<String> parentOrgDeptIds) {
        return selectList(OrgDeptDO::getParentOrgDeptId, parentOrgDeptIds);
    }

    default List<OrgDeptDO> listByDeptNames(Collection<String> deptNames){
        return selectList(new LambdaQueryWrapperX<OrgDeptDO>()
                .in(OrgDeptDO::getOrgDeptName,deptNames)
                .eq(OrgDeptDO::getOrgDeptStatus,CommonStatusEnum.ENABLE));
    }

    default PageResult<OrgDeptDO> selectDeptByPage(OrgDeptPageReq reqVO){
        return selectPage(reqVO, new LambdaQueryWrapperX<OrgDeptDO>()
                .likeIfPresent(OrgDeptDO::getOrgDeptName, reqVO.getOrgDeptName())
                .eqIfPresent(OrgDeptDO::getOrgDeptStatus, reqVO.getOrgDeptStatus())
                .orderByAsc(OrgDeptDO::getOrgDeptSort));
    }
}
